import { setItem, getItem } from './../../utils/auth'

import { createSlice, PayloadAction } from '@reduxjs/toolkit'

import { RootState } from '../index'

interface InitialStateType {
  i18n: string
}
// 使用该类型定义初始 state
const initialState: InitialStateType = {
  i18n: getItem('lang') || 'zh'
}
const config = createSlice({
  name: 'config',
  initialState,
  reducers: {
    i18nConfig(state, action: PayloadAction<string>) {
      state.i18n = action.payload
      setItem('lang', action.payload)
    }
  }
})
// 导出state
export const i18n_config = (state: RootState) => state.config.i18n

// 导出actions
export const { i18nConfig } = config.actions
export default config.reducer
